- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBinaryTreeClient.java
90 lines (65 loc) Β· 2.89 KB
/
BinaryTreeClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
packagesection24_BinaryTree;
publicclassBinaryTreeClient {
publicstaticvoidmain(String[] args) {
BinaryTreebtree = newBinaryTree();
/* test input: 10 Y 20 Y 40 N N Y 50 N N Y 30 N Y 60 N N */
btree.display();
System.out.println("size: " + btree.size());
/* test input: 10 Y 20 Y 40 N N Y 580 N N Y 30 N Y 60 N N */
System.out.println("max: " + btree.max());
System.out.println("found 10: " + btree.find(10));
System.out.println("found 50: " + btree.find(50));
System.out.println("found 60: " + btree.find(30));
System.out.println("found 40: " + btree.find(40));
System.out.println("found 500: " + btree.find(500));
System.out.println("found -1: " + btree.find(-1));
/* test input: 10 Y 20 Y 40 N N Y 50 N N Y 30 N Y 60 N Y 111 N N */
System.out.println("height: " + btree.height());
/* test input: 10 Y 20 Y 40 N N Y 50 N Y 70 N N Y 30 N Y 60 N N */
/*
* test input: 10 Y 20 Y 40 Y 60 Y 80 N N N N Y 50 N Y 70 N Y 90 N N Y
* 30 N N
*/
System.out.println("diameter: " + btree.diameter());
System.out.println("diameter: " + btree.diameter2());
System.out.println("diameter: " + btree.diameter3());
/* test input: 10 Y 20 Y 40 N N Y 50 N N Y 30 N Y 60 N N */
System.out.println("is Balanced: " + btree.isBalanced());
System.out.println("is Balanced: " + btree.isBalanced3());
/* test input: 10 Y 20 Y 40 N N Y 50 N N Y 30 N Y 70 N N */
System.out.println("\npre-order traversal");
btree.preorder();
System.out.println("\nin-order traverasl");
btree.inorder();
System.out.println("\npost-order traversal");
btree.postorder();
System.out.println("\npreorder iterative");
btree.preorderIterative();
/* test input: 100 Y 20 Y -40 N N Y 50 N N Y -30 N Y 70 N N */
System.out.println("\n\nSum: " + btree.sum());
/*
* test input: 10 Y 20 Y 40 Y 2 N N Y 3 N N Y -60 N N Y -30 Y 50 Y 60 Y
* 80 N N N Y 70 N N Y -100 N N
*/
System.out.println("\nmax subtree sum: " + btree.maxSubtreeSum1());
System.out.println("\nmax subtree sum: " + btree.maxSubtreeSum2());
System.out.println("\nmax subtree sum: " + btree.maxSubtreeSum3());
System.out.println("\nTree construction using preorder & inorder traversal");
int[] preorderTraversal = { 10, 20, 40, 30, 50, 60, 70 };
int[] inorderTraversal = { 40, 20, 10, 50, 30, 60, 70 };
BinaryTreebtree2 = newBinaryTree(inorderTraversal, preorderTraversal);
btree2.display();
int[] in = { 40, 20, 50, 10, 30, 70 };
int[] pre = { 10, 20, 40, 50, 30, 70 };
BinaryTreebtree3 = newBinaryTree(in, pre);
btree3.display();
System.out.println("\n\nFlip Equivalent binary tree");
/* test input: 1 Y 2 Y 4 N N Y 5 Y 7 N N Y 8 N N Y 3 Y 6 N N N */
BinaryTreebt1 = newBinaryTree();
bt1.display();
/* test input: 1 Y 3 N Y 6 N N Y 2 Y 4 N N Y 5 Y 8 N N Y 7 N N */
BinaryTreebt2 = newBinaryTree();
bt2.display();
System.out.println("is flip equivalence: " + bt1.flipEquivalent(bt2));
}
}